package org.osomit.sacct.session.registry.impl;

import org.osomit.sacct.session.token.impl.SessionToken;
import org.osomit.sacct.session.registry.iface.TokenRegistry;

import java.util.*;

/**
 * @author Jian Fang (John.Jian.Fang@gmail.com)
 *
 * Date: Jul 2, 2009
 */
public class InMemorySessionTokenRegistryImpl implements TokenRegistry<SessionToken> {

    private final Map<String, SessionToken> m = Collections.synchronizedMap(new HashMap<String, SessionToken>());

    public void addToken(SessionToken token) {
         synchronized (this.m){
             this.m.put(token.getId(), token);
         }
    }

    public void updateToken(SessionToken token) {
        synchronized (this.m){
             this.m.put(token.getId(), token);
         }
    }

    public SessionToken getToken(String id) {
        synchronized (this.m){
             return this.m.get(id);
         }
    }

    public void deleteToken(String id) {
        synchronized (this.m){
             this.m.remove(id);
        }
    }

    public int getTokenNumber() {
        synchronized (this.m){
            return this.m.size();
        }
    }

    public Collection<SessionToken> getAllTokens() {
        synchronized (this.m){
            return this.m.values();
        }
    }

    public void clear() {
        synchronized (this.m){
            this.m.clear();
        }
    }
}
